// pages/message/message.js
var Bmob = require('../../dist/Bmob-1.6.0.min.js');
var app = getApp()
const {
  $Message
} = require('../../dist/base/index');
export default {
  config: {
    usingComponents: {
      'wxc-abnor': '../..//dist/package/@minui/wxc-abnor'
    }
  },
  data: {},
  methods: {}
}
Page({
  array: [],

  /**
   * 页面的初始数据
   */
  data: {
    usrarray: [
      {
        imageurl: "http://npic7.edushi.com/cn/zixun/zh-chs/2018-06/15/9a9ce41b2071431ab76a1cf1bdeb345c.jpg",
        name: "_abu"
      },
      {
        avatarurl: "http://npic7.edushi.com/cn/zixun/zh-chs/2018-06/15/9a9ce41b2071431ab76a1cf1bdeb345c.jpg",
        name: "_abu"
      }, {
        imageurl: "http://npic7.edushi.com/cn/zixun/zh-chs/2018-06/15/9a9ce41b2071431ab76a1cf1bdeb345c.jpg",
        name: "_abu"
      }
    ],
    array: [
      {
        imageurl: "http://npic7.edushi.com/cn/zixun/zh-chs/2018-06/15/9a9ce41b2071431ab76a1cf1bdeb345c.jpg",
        name: "_abu"
      },
      {
        avatarurl: "http://npic7.edushi.com/cn/zixun/zh-chs/2018-06/15/9a9ce41b2071431ab76a1cf1bdeb345c.jpg",
        name: "_abu"
      }, {
        imageurl: "http://npic7.edushi.com/cn/zixun/zh-chs/2018-06/15/9a9ce41b2071431ab76a1cf1bdeb345c.jpg",
        name: "_abu"
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(this.data.array.length)
    let that = this
    wx.setNavigationBarTitle({
      title: '我的小组'
    });
    that.setData({
      // 这是调试用的 正式版本请将下列代码取消屏蔽
      objectId_user: app.globalData.authData.objectId
      // objectId_user: '6a634ce6a9'
    })
    //通过_User查找对应的Postings，然后反向查找对应Postings被follow的UserData
    const pointer = Bmob.Pointer('_User')
    const poiID = pointer.set(that.data.objectId_user)

    const query = Bmob.Query('Postings')
    // query.equalTo("user", "==", poiID);
    query.order("-createdAt")
    query.find().then(res => {
      let array = new Array(res.length)
      that.setData({
        array: array
      })
      for (let i = 0; i < res.length; i++) {
        let item = res[i]
        console.log(item)
        const query2 = Bmob.Query("UserData")
        query2.statTo("where", '{"include":{"$inQuery":{"where":{"objectId":"' + item.objectId + '"},"className":"Postings"}}}');
        // query2.statTo("where", '{"$relatedTo":{"object":{"__type":"Pointer","className":"Postings","objectId":"' + item.objectId + '"},"key":"follow"}}');
        // query2.equalTo("objectId", "==", dataId)

        query2.find().then(res3 => {
          // console.log("查找有没有符合条件的UserData", res3)
          array = that.data.array
          if (res3.length) {
            console.log(item.objectId, res3)
            array[i] = {
              Posting: item,
              users: res3,
              my: item.user.objectId == that.data.objectId_user
            }
          }
          that.setData({
            array: array
          })
          //   if(res3.length){
          //     console.log("查询到相关信息")
          //   }
          //   else{
          //     console.log("没有查询到相关信息")
          //   }
        })
      }

    })



  },
  
  copy:function(e){
    console.log(e.currentTarget.id)
    wx.setClipboardData({
      data: e.currentTarget.id,
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {
            console.log(res.data) // data
          }
        })
      }
    })
  },
  delete: function (objectId, objectId_user) {
    console.log("取消申请" + objectId)
    console.log("userid", objectId_user)
    const query = Bmob.Query('UserData');
    // query.statTo("where", '{"user":{"$inQuery":{"where":{"objectId":"' + objectId_user + '"},"className":"_User"}}}');
    const relation = Bmob.Relation('Postings')
    const relID = relation.remove([objectId])
    query.get(objectId_user).then(res => {
      console.log("找到用户信息", res)
      res.set('include', relID)
      res.save().then(res => {
        // 成功删除
        console.log('信息删除成功', res)
      }).catch(err => {
        console.log(err)
      });
    }).catch(err => {
      console.log(err)
    })
  },
  getin: function (objectId, objectId_user) {
    console.log("申请加入group" + objectId)
    console.log("userid", objectId_user)
    const query = Bmob.Query('UserData');
    // query.statTo("where", '{"user":{"$inQuery":{"where":{"objectId":"' + objectId_user + '"},"className":"_User"}}}');
    const relation = Bmob.Relation('Postings')
    const relID = relation.add([objectId])
    query.get(objectId_user).then(res => {
      console.log("找到用户信息", res)
      res.set('include', relID)
      res.save().then(res => {
        // 成功修改
        console.log('信息加入成功', res)
      }).catch(err => {
        console.log(err)
      });
    }).catch(err => {
      console.log(err)
    })
    // const query = Bmob.Query("UserData")
    //查找对应的UserData
    // query1.statTo("where", '{"$relatedTo":{"object":{"__type":"Pointer","className":"Postings","objectId":"' + res.objectId + '"},"key":"tag"}}');
    // query1.statTo("where", '{"tag":{"$inQuery":{"where":{"objectId":"'++'"},"className":"_User"}}}');
    // query1.find().then(res1 => {

    // })
  },
  sayYes: function (e) {
    var index = parseInt(e.currentTarget.id[0])
    var index_user = parseInt(e.currentTarget.id[1])
    var objectId = this.data.array[index].Posting.objectId
    var objectId_user = this.data.array[index].users[index_user].objectId
    // console.log(objectId, objectId_user)
    this.getin(objectId, objectId_user)

  },
  sayNo: function (e) {
    var index = parseInt(e.currentTarget.id[0])
    var index_user = parseInt(e.currentTarget.id[1])
    var objectId = this.data.array[index].Posting.objectId
    var objectId_user = this.data.array[index].users[index_user].objectId
    this.delete(objectId, objectId_user)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})